#include "ui_timer.h"
#include "ui_message_server.h"


namespace UI
{

Uint32 TimerCallback(Uint32 Interval, void* param)
{
	return static_cast<UI_Timer*>(param)->TimerHit(Interval);
}


UI_Timer::UI_Timer(UI_MessageClient* pOwner) :
	m_TimerID(0),
	m_bAutoRestart(false),
	m_iCounter(0),
	m_pOwner(pOwner)
{ }


UI_Timer::~UI_Timer(void)
{
	StopTimer();
}


void UI_Timer::StartTimer(unsigned long int Interval, bool bAutoRestart)
{
	m_bAutoRestart = bAutoRestart;
	if (m_TimerID != 0)
	{
		StopTimer();
	}
	m_TimerID = SDL_AddTimer(Interval, &TimerCallback, this);
}


void UI_Timer::StopTimer(void)
{
	if (m_TimerID)
	{
		SDL_RemoveTimer(m_TimerID);
		m_TimerID = 0;
	}
}


Uint32 UI_Timer::TimerHit(Uint32 Interval)
{
	m_iCounter++;
	UI_MessageServer::Instance().QueueMessage(new TIntMessage(UI_Message::CTRL_TIMER, m_pOwner, this, m_iCounter));
	if (!m_bAutoRestart)
	{
		StopTimer();
	}

	return Interval;
}


bool UI_Timer::HandleMessage(UI_Message* /*pMessage*/)
{
	bool bHandled = false;

	return bHandled;
}

}

